package com.revai.engine.basic.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONObject;

public class ServiceUtil {

    private static String BaseUrl = "https://new-nmrstest.loongair.cn/webapi/ms-provider-atom-serve-nmrs-base";
    public static void main(String[] args) {
		try {
            JSONObject reqJson = new JSONObject();
            reqJson.put("processKey", "vmrs-partnumber-flow");
            reqJson.put("businessId", 15L);
            LocalDateTime cutoffTime = LocalDateTime.now().plusDays(7L);
            reqJson.put("cutoffTime", cutoffTime);

            String authToken = "66986c5f-e577-4411-b7a8-e9308462a97f";

            ServiceUtil.doHttp("/wfCommonProcess/v1/startWorkflow", reqJson, authToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
 
    public static JSONObject doHttp(String uri, JSONObject reqestBody, String authToken) throws Exception {
        return new JSONObject();
    }

    /**
     * 使用反射，遍历model所有属性，执行criteria.andEqualTo()方法
     * 
     * @param <T>
     * @param criteriaClass Example.Criteria的class
     * @param criteria      Example.Criteria查询对象
     * @param clazz         model的Class
     * @param model         model实例
     * @return
     */
    
    public static void andAllEqualTo(Class<?> criteriaClass, Object criteria, Class<?> modelClass, Object model) {
        if (criteria == null || model == null) {
            return;
        }
        Method[] methods = modelClass.getMethods();
        // 遍历方法
        for (Method method : methods) {
            // 判断是否为 Getter 方法
            if (isGetter(method)) {
                try {
                    // 获取属性名
                    String fieldName = getFieldName(method);
                    // 获取属性值
                    Object fieldValue = invokeGetterMethod(method, model);
                    if (fieldValue != null) {
                        // 获取字段
                        Field field = modelClass.getDeclaredField(fieldName);
                        // 字段类型
                        // example.andLike(key, query.getStr(key));
                        Class type = field.getType();
                        String upperFieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

                        if (type.equals(String.class)) {
                            // andNameLike
                            ReflectUtil.invoke(criteria, "and" + upperFieldName + "Like", fieldValue + "%");
                            // Method equalMethod = criteriaClass.getMethod("and" + upperFieldName + "andLike", type);
                        } else {
                            // 获取Criteria的addEqualTo方法
                            Method equalMethod = criteriaClass.getMethod("and" + upperFieldName + "EqualTo", type);
                            equalMethod.invoke(criteria, fieldValue);
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 根据modelName获取查询example对象
     * @param modelName
     * @return
     * @throws Exception
     */
    public static Object getExampleCriteria(String modelName) throws Exception {
        Class<?> exampleClass = Class.forName(ConstConfig.BASEPACKAGEMODEL + modelName + "Example");
        Object exampleObject = exampleClass.newInstance();
        Method criteriaMethod = exampleClass.getMethod("createCriteria");
        return criteriaMethod.invoke(exampleObject);
    }

    /**
     * 根据modelName获取查询exampleCriteria类
     * @param modelName
     * @return
     * @throws Exception
     */
    public static Class<?> getExampleClass(String modelName) throws Exception {
        Class<?> exampleClass = Class.forName(ConstConfig.BASEPACKAGEMODEL + modelName + "Example");
        Class<?>[] clazzes = exampleClass.getDeclaredClasses();
        for (Class<?> clazz : clazzes) {
            if ((ConstConfig.BASEPACKAGEMODEL + modelName + "Example$Criteria").equals(clazz.getName())) {
                return clazz;
            }
        }
        return null;
    }

    // 判断方法是否为 Getter 方法
    private static boolean isGetter(Method method) {
        return method.getName().startsWith("get") && method.getParameterCount() == 0
                && !"getClass".equals(method.getName())
                && !void.class.equals(method.getReturnType());
    }

    // 获取属性名
    private static String getFieldName(Method method) {
        String methodName = method.getName();
        return methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
    }

    // 调用 Getter 方法获取属性值
    private static Object invokeGetterMethod(Method method, Object target) {
        try {
            return method.invoke(target);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
